package com.sduept.nwld.dataserver.rest.daily;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.cim.model.core.Substation;
import com.sduept.core.dao.SuperDAO;
import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.nwld.dataserver.manager.protection.ProtectDefectManager;
import com.sduept.nwld.dataserver.model.daily.ProtectDefect;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 保护缺陷及处理情况
 */
@RestController
@RequestMapping("/protectDefect")
public class ProtectDefectService {
    @Autowired
    private SuperDAO dao;

    @Autowired
    private ProtectDefectManager pdm;
    @Autowired
    private MapDataCache cc;

    @GetMapping("/getProtectDefect/{start}/{end}")
    public String getProtectDefect(@PathVariable("start") String start,
                                   @PathVariable("end") String end) {
        Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymd);
        Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymd);
        Calendar c = Calendar.getInstance();
        c.setTime(e);
        c.add(Calendar.DAY_OF_YEAR, 1);
        c.add(Calendar.SECOND, -1);
        e = c.getTime();
        List<ProtectDefect> objs = pdm.getProtectDefect(s, e);

        Map<String, Substation> stationMap = cc.getStationid_map();
        JSONArray arr = new JSONArray();
        for (ProtectDefect o : objs) {
            JSONObject jo = JSONObject.parseObject(JSONObject.toJSONString(o));
            Substation st = stationMap.get(jo.getString("substationId"));
            if (st != null) {
                jo.put("lon", st.getLongitude());
                jo.put("lat", st.getLatitude());
            }
            arr.add(jo);
        }
        return JSONArray.toJSONString(arr);
    }


    @GetMapping("/getProtectDefectByStationId/{start}/{end}/{stationId}")
    public String getProtectDefectByStationId(@PathVariable("start") String start,
                                              @PathVariable("end") String end,
                                              @PathVariable("stationId") String stationId) {
        Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymd);
        Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymd);
        Calendar c = Calendar.getInstance();
        c.setTime(e);
        c.add(Calendar.DAY_OF_YEAR, 1);
        c.add(Calendar.SECOND, -1);
        e = c.getTime();
        List<ProtectDefect> objs = pdm.getProtectDefect(s, e, stationId);
        return JSONArray.toJSONString(objs);
    }
}
